package com.gx.smart.building.ui.assets

import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gx.smart.base.BaseMVVMActivityV3
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivityAssetsDetailBinding
import com.gx.smart.building.ui.assets.adapter.PageAdapter
import com.gx.smart.building.ui.assets.fragment.AssetsDetailFragment
import com.gx.smart.building.ui.assets.fragment.AssetsInspectionRecordFragment
import com.gx.smart.building.ui.assets.fragment.AssetsMaintainRecordFragment
import com.gx.smart.building.ui.assets.fragment.AssetsRunningStateFragment
import com.gx.smart.building.ui.assets.viewmodel.AssetsViewModel

class AssetsDetailActivity : BaseMVVMActivityV3<AssetsViewModel>() {
    private lateinit var viewBinding: ActivityAssetsDetailBinding

    override fun onBindViewModel(): Class<AssetsViewModel> = AssetsViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_assets_detail

    override fun getToolbarTitle(): String = "资产详情"

    override fun initContentView(root: ViewGroup) {
        viewBinding = ActivityAssetsDetailBinding.inflate(layoutInflater, root, true)
    }

    override fun initData() {
        var titles = resources.getStringArray(R.array.assets).toList()
        val adapter = PageAdapter(this)
        viewBinding.viewPager.adapter = adapter
        viewBinding.viewPager.isUserInputEnabled = true

        adapter.add(AssetsDetailFragment.newInstance())
        if (intent.hasExtra("deviceId")) {
            adapter.add(AssetsRunningStateFragment.newInstance())
        } else {
            titles = titles.filterIndexed { index, s -> index != 1 }
        }
        adapter.add(AssetsInspectionRecordFragment.newInstance())
        adapter.add(AssetsMaintainRecordFragment.newInstance())
        val mediator = TabLayoutMediator(
            viewBinding.tabLayout, viewBinding.viewPager
        ) { tab: TabLayout.Tab, position: Int ->
            tab.text = titles[position]
        }
        mediator.attach()
    }
}